package com.sadam.ui4.FragmentHomePage;

import com.sadam.ui4.Data.User;
import com.sadam.ui4.Data.Video;

import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

//混合排序算法
public class VideoRecommendationAlgorithm {

    /**
     * 综合评分算法 = 热度分 + 时间衰减分
     */
    public static double calculateVideoScore(Video video, User currentUser) {
        double hotScore = calculateHotScore(video);
        double timeScore = calculateTimeDecayScore(video);

        // 权重分配（可根据实际情况调整）
        return 0.5 * hotScore + 0.5 * timeScore;
    }

    /**
     * 热度分 = 点赞权重 + 评论权重
     */
    private static double calculateHotScore(Video video) {
        double likeWeight = Math.log1p(video.getLikeCount()) * 2.0; // 对数压缩
        double commentWeight = Math.log1p(video.getCommentCount()) * 1.5;

        return likeWeight + commentWeight;
    }

    /**
     * 时间衰减分：新视频有更高权重
     */
    private static double calculateTimeDecayScore(Video video) {
        // 假设video有发布时间字段
        long publishTime = video.getPublishTime();
        long currentTime = System.currentTimeMillis();
        long hoursSincePublish = (currentTime - publishTime) / (1000 * 3600);
        // 输出日志
        Logger logger = Logger.getLogger("TimeDecayLogger");
        logger.log(Level.INFO, "Hours since publish: {0}", hoursSincePublish);
        // 指数衰减：24小时内衰减较慢
        return Math.exp(-hoursSincePublish / 48.0); // 48小时半衰期
    }

}
